package main

import (
	"github.com/micro/cli/v2"
	"github.com/micro/go-micro/v2"
	"log"
	"runtime"

	"kop/framework/broker"
	"kop/framework/conf"
	"kop/framework/wrapper"
	"kop/libs/database"
	"kop/pb/service"
	log2 "kop/util/log"

	handler3 "kop/modules/achievement/handler"
	models3 "kop/modules/achievement/models"
	questH "kop/modules/quest/handler"
	questM "kop/modules/quest/models"
	roleH "kop/modules/role/handler"
	roleM "kop/modules/role/models"
	stageH "kop/modules/stage/handler"
	stageM "kop/modules/stage/models"
)

func main() {

	runtime.GOMAXPROCS(2)

	conf.Init()
	log2.SetLevel(conf.Log.Level)

	// New Service
	var srv = micro.NewService(
		micro.Name("go.micro.srv.role"),
		micro.WrapHandler(wrapper.NewLogWrapper),
		micro.Action(func(c *cli.Context) error {
			return broker.Init(c.String("broker_address"))
		}),
		micro.AfterStart(
			func() error {
				log.Println("启动角色服务器.")
				return nil
			}),
	)

	srv.Init()

	database.DB = database.Open(conf.DataBase.Dsn, conf.DataBase.MaxIdle, conf.DataBase.MaxOpen, conf.DataBase.Log)
	defer database.DB.Close()

	roleM.AutoMigrate()
	stageM.AutoMigrate(database.DB)
	questM.AutoMigrate(database.DB)
	models3.AutoMigrate()

	_ = service.RegisterRoleServiceHandler(srv.Server(), new(roleH.RoleServiceHandler))
	_ = service.RegisterStageServiceHandler(srv.Server(), new(stageH.StageServiceHandler))
	_ = service.RegisterQuestServiceHandler(srv.Server(), new(questH.QuestServiceHandler))
	_ = service.RegisterAchievementServiceHandler(srv.Server(), new(handler3.AchievementServiceHandler))

	// Run service
	if err := srv.Run(); err != nil {
		panic(err)
	}
}
